<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * @author Anh Nguyen <duyanhnguyen@vccorp.vn>
 * @copyright 2012
 * @property CI_DB_active_record $db
 */
class Post_model extends CI_Model
{

    public function getPostByCatId($catId, $numPost = '', $start = '', $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.title_$langCode as 'title', post.description_$langCode as 'description', post.link_img, post.modified, post.price, category.name_$langCode as cat_title, category.type, post.phone");
        $this->db->from('post');
        $this->db->join('category', 'category.id = post.parent_id');
        $this->db->where(array(
            'post.parent_id' => $catId,
            'post.status' => 1
        ));
        $this->db->order_by('post.modified', 'desc');
        if ($numPost != '' || $start != '')
            $this->db->limit($numPost, $start);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getPostLikeKeyword($keyword, $numPost = '', $start = '', $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.title_$langCode as 'title', post.description_$langCode as 'description', post.link_img, post.modified, post.price, category.type");
        $this->db->distinct();
        $this->db->from('post');
        $this->db->join('category', 'category.id = post.parent_id');
        $this->db->like("post.title_$langCode", $keyword);
        $this->db->or_like("post.description_$langCode", $keyword);
        $this->db->or_like("post.content_$langCode", $keyword);
        $this->db->where(array(
            'post.status' => 1
        ));
        $this->db->order_by('type', 'asc');
//        $this->db->order_by('modified', 'desc');
        if ($numPost != '' || $start != '')
            $this->db->limit($numPost, $start);
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getGoodPost($langCode)
    {
        $return = array();
        $this->db->select("id, title_$langCode as 'title', description_$langCode as 'description', link_img");
        $this->db->from('post');
        $this->db->where(array(
            'good_post' => 1,
            'status' => 1
        ));
        $this->db->order_by('modified', 'desc');
        $rs = $this->db->get();
        $return = $rs->result_array();
        $rs->free_result();
        return $return;
    }

    public function getNumPostByCatId($catId)
    {
        $this->db->from('post');
        $this->db->where(array(
            'parent_id' => $catId,
            'status' => 1
        ));
        return $this->db->count_all_results();
    }

    public function getNumPostLikeKeyword($keyword, $langCode)
    {
        $this->db->distinct();
        $this->db->from('post');
        $this->db->like("title_$langCode", $keyword);
        $this->db->or_like("description_$langCode", $keyword);
        $this->db->or_like("content_$langCode", $keyword);
        $this->db->where(array(
            'status' => 1
        ));
        return $this->db->count_all_results();
    }

    public function getInfoById($id, $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.parent_id, post.title_$langCode as 'title', post.description_$langCode as 'description', post.content_$langCode as 'content', post.modified, post.link_img, post.price, category.name_$langCode as 'parent_name', category.type, post.vote, post.count_vote, post.phone");
        $this->db->from('post');
        $this->db->join('category', 'post.parent_id = category.id', 'inner');
        $this->db->where(array(
            'post.id' => $id,
            'post.status' => 1
        ));
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }

    public function getMorePostWithType($postId, $type, $langCode)
    {
        $return = array();
        $this->db->select("post.id, post.parent_id, post.title_$langCode as 'title', post.description_$langCode as 'description',category.name_$langCode as 'parent_name', post.modified, post.price, post.link_img");
        $this->db->from('post');
        $this->db->join('category', 'post.parent_id = category.id', 'inner');
        $this->db->where(array(
            'post.status' => 1,
            'category.type' => $type,
            'post.id !=' => $postId
        ));
        $this->db->order_by('modified', 'desc');
        $this->db->limit(5);
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }

    public function getPostByType($type, $langCode, $order, $ordertype = 'asc', $limit = '')
    {
        $return = array();
        $this->db->select("post.id, post.parent_id, post.title_$langCode as 'title', post.description_$langCode as 'description', post.content_$langCode as 'content', category.name_$langCode as 'parent_name', post.modified, post.price, post.link_img");
        $this->db->from('post');
        $this->db->join('category', 'post.parent_id = category.id', 'inner');
        $this->db->where(array(
            'post.status' => 1,
            'category.type' => $type
        ));
        $this->db->order_by($order, $ordertype);
        if ($limit != '') {
            $this->db->limit($limit);
        }
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }

    public function votePost($id, $vote)
    {
        $return = array();
        $update = array(
            'vote' => "vote + $vote",
            'count_vote' => 'count_vote + 1'
        );
        $this->db->set('vote', "vote+$vote", FALSE);
        $this->db->set('count_vote', 'count_vote+1', FALSE);
        $this->db->where('id', $id);
        $this->db->update('post');
        $this->db->select('count_vote')->from('post')->where('id', $id);
        $rs = $this->db->get();
        if ($rs->num_rows() == 0) {
            $rs->free_result();
            return false;
        } else {
            $return = $rs->result_array();
            $rs->free_result();
            return $return;
        }
    }

}

?>
